﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Reflection;

namespace MGGameWS
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MGGameService" in code, svc and config file together.
    public class MGGameService : IMGGameService
    {
        public void RecordData(Stream data)
        {
            
            var jsonReader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas());

            XElement root = XElement.Load(jsonReader);

            // The fields we'd like to extract
            XElement file = root.Element("file");
            XElement folder = root.Element("folder");  
            XElement msgType = root.Element("msgType");
            XElement msgName = root.Element("msgName");
            XElement content = root.Element("content");
            var fileName = file.FirstNode;
            var folderName = folder.FirstNode;
            string filepath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
            filepath = Path.GetDirectoryName(filepath) + "\\Experiment1\\player_id_" + folderName + "\\Game_";
            if (!Directory.Exists(filepath))
                Directory.CreateDirectory(filepath);           
            filepath = filepath + "\\staticInfo_" + fileName + ".txt";    

            var writer = new StreamWriter(filepath, true, Encoding.Unicode);
            var messageToWrite = "time: " + DateTime.Now + "\t" + "msgType: " + msgType.FirstNode + "\t" + "msgName: " + msgName.FirstNode + "\t" + "content: " + content.FirstNode ;
            writer.Write(messageToWrite);
            writer.WriteLine();
            writer.Flush();
            writer.Close();
        }

        public void HeaderData(Stream data)
        {

            var jsonReader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas());

            XElement root = XElement.Load(jsonReader);

            // The fields we'd like to extract
            XElement file = root.Element("file");
            XElement folder = root.Element("folder");
            XElement age = root.Element("age");
            XElement gender = root.Element("gender");
            XElement state = root.Element("state");
            var fileName = file.FirstNode;
            var folderName = folder.FirstNode;
            string filepath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
            filepath = Path.GetDirectoryName(filepath) + "\\Experiment1\\player_id_" + folderName;
            if (!Directory.Exists(filepath))
                Directory.CreateDirectory(filepath);
            filepath = filepath + "\\playerInfo";
            if (!Directory.Exists(filepath))
                Directory.CreateDirectory(filepath);          
            filepath = filepath + "\\demographic.txt";
            var writer = new StreamWriter(filepath, true, Encoding.Unicode);
            writer.Write("HEADER");
            writer.WriteLine();
            writer.WriteLine();
            var messageToWrite = "Start time: " + DateTime.Now + "; " + "age: " + age.FirstNode + "; " + "gender: " + gender.FirstNode + "; " + "state: " + state.FirstNode + "; ";
            writer.Write(messageToWrite);
            writer.WriteLine();
          
            writer.Flush();
            writer.Close();
        }
    }
}
